package com.heima.zuoye.day10;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        FileReader fir = new FileReader("D:\\JAVA\\JavaSE进阶-相关资料\\课后作业\\day10\\hero.txt", Charset.forName("GBk"));
        BufferedReader br = new BufferedReader(fir);
        TreeSet<hero> list = new TreeSet<>(new Comparator<hero>() {
            @Override
            public int compare(hero o1, hero o2) {
                int num = Double.compare(o2.getShi(), o1.getShi());
                if (num != 0) {
                    return num;
                } else {
                    return o2.getName().compareTo(o1.getName());
                }
            }
        });
        String s = br.readLine();
        String str;
        while ((str = br.readLine()) != null) {
            String[] split = str.split(",+");
            hero h = new hero(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
            list.add(h);
        }
        br.close();
        for (hero hero : list) {
            System.out.println(hero);
        }
        FileWriter fw = new FileWriter("D:\\JAVA\\JavaSE进阶-相关资料\\课后作业\\day10\\hero.txt", Charset.forName("GBk"));
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(s);
        bw.newLine();
        for (hero hero : list) {
            bw.write(hero.getName() + "," + hero.getGong() + "," + hero.getSheng() + "," + hero.getXiao() + "," + hero.getShi());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
